package com.terry.trace.plugin;

import com.terry.trace.core.LogTagTool;
import com.terry.trace.core.TraceLogger;
import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * feign拦截器 用来透传追踪数据到下一节点
 * @author TerryJiang
 */
public class TraceFeignInterceptor implements RequestInterceptor {

    private static TraceLogger logger = TraceLogger.getLogger(TraceFeignInterceptor.class);

    /**
     * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        try {
            String flowNo = LogTagTool.getContextFlowNo();
            template.header(LogTagTool.LOG_TAG_FLOW_NO, flowNo);
        } catch (Exception exception) {
            logger.warn("TraceFeignInterceptor apply error!", exception);
        }
    }
}
